package com.mystyle.le.weekproject;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mystyle.le.weekproject.fragment.GameFragment;
import com.mystyle.le.weekproject.fragment.HomeFragment;
import com.mystyle.le.weekproject.fragment.ManagerFragment;
import com.mystyle.le.weekproject.fragment.SoftFragment;

import java.util.ArrayList;
import java.util.List;

public class HelloActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private List<Fragment> list = new ArrayList<>();
    private int current;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);
        initView();
        initFragment();
    }

    private void initFragment() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.homePage_button:
                        switchFraagment(0);


                        break;
                    case R.id.search_button:
                        switchFraagment(1);

                        break;
                    case R.id.information_button:
                        switchFraagment(3);

                        break;
                    case R.id.manage_button:
                        switchFraagment(2);

                        break;
                }
            }
        });
    }

    private void initView() {
        radioGroup = ((RadioGroup) findViewById(R.id.title_radioGroup));
        HomeFragment homeFragment = new HomeFragment();
        SoftFragment softFragment = new SoftFragment();
        ManagerFragment managerFragment = new ManagerFragment();
        GameFragment gameFragment = new GameFragment();
        list.add(homeFragment);
        list.add(softFragment);
        list.add(managerFragment);
        list.add(gameFragment);
        getSupportFragmentManager().beginTransaction().add(R.id.title_First,list.get(0)).commit();
        initFragment();
    }



    private void switchFraagment(int i) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //点击的Fragment目标
        Fragment fragment = list.get(i);
        //当前的fragment
        Fragment fragment1 = list.get(current);
        //点击的 按钮对象的Fragment 存在    show()展示出来  隐藏当前的Fragment
        if (!fragment.isAdded()){
            fragmentTransaction.add(R.id.title_First,fragment).hide(fragment1).commit();
        }else {
            fragmentTransaction.show(fragment).hide(fragment1).commit();
        }
        current = i;
    }
}
